const Koa = require('koa');
const Router = require('koa-router');
const axios = require('axios');
const bodyParser = require('koa-bodyparser');

const app = new Koa();
const router = new Router();

app.use(bodyParser());

router.post('/forward', async (ctx) => {
  try {
    // 获取原始请求中的 token
    // const token = ctx.request.headers['authorization'] || ''; 
    let message = ctx.request.body.message
    console.log('参数1:',message);

    let cookie = {
      cf_clearance: "u8VaGROE7U7qVevS42lVhKAyqeVgrPVJXa8DyPgeam8-1679834376-0-1-af92601c.e3a69f92.f8933ffc-250",
      "__Secure-next-auth.session-token": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..UKtvTk18Ckac9DaR.LKW9jt64c7punXf6oP2TzlbqM64n9OuSr0QJQMY0YbnPXKXUQPHmvrnmkBKY8pSK3oIFnEYx_DGMQhKOYpy0brewoj3hL1S1WPaYBmXbD1AtZDdU18SpepMBrvbrSfjO3Wlm8meejWNdNowixgLg7vxta-RyEefO-jYpSGuNE1g14F6Lbqm-xj4itQDPE9P7paFSZCE_CQYVr2TfGB-8tQvomH5OQm98n2231bLlr4P356yzJhVprOgFTqCzMk7otRDBOUxNlZuG-MH4T9mVzKAjnJapqbGOTUpaTzx3UFd3p1cydA4_Ldws8zAMzTT8Q7tnXRhpiqTl0PjZlVC9pT5EotYDERy0BIK8dXjL71yDukFDq300j7BoAeig58f1eb38ln1ot8NtOEI1ftfyDOXJT683qRLDZ65I-VhD_dJORmTiAKWDVCcT3bSyG1v6XGwXMmtRoGa8s9-3c2LjVRJXYNokQKOfLJ9GPDjmimci5ozUp5zmzReT0ftK_I-cWM22pl98QM3gGWFK-kD0KIyklbVy2bTDFUA7v8759CTHj3gn7grhjRrvrI6EilufUYm_9U4Sm4JisDL7-C3mKq1BeS415NATijrVpPOEvljN6FxcZQz2WTBi-8ti8fp19u46b_pZmaISO6hVSsdUHw2l8ylvQDN9S5cy7yp1Qxu_4GfNeHkSyOsCC7ULEwvIq7n1FiFGOjZS_fAcy-cWkoXD_LGdMd6wnvIgKKjIjxK-8Cu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MJPzW9hVdyIVEiCNrVbKtxh6FwkmAq5hCi9EryuP00yOf-zNwwp_hdqErYqKax23NN8QXIYkbLfL-wbP158YyV7q3egqWIo3dZ_CkhQhkgykrkXYnJVZQSaqGFQmL9RekoYk1kmHlR2YsCfVUgQd6JcZjR5xm_a6YAZSiygmQQcPJOK6K-OxlR_zMWlonyE7OVAyGIbmiJs1CVIW-DADP2peeuU9kMR4H6HVrMlolQvA8uvBn2j8TietVYFX_QU25oHqNT6Kfq-LcqGks7TOl7RGV5ZDbnRaX4AxwC6FnOcobeg0eeySUKmey5L42bajNdoEIEeYA4M9588Z_U-f_inROG4uFKu4ZlF2elkZzyZctJeJY_En-UeA4FDbEnqnOoyeC4vItg7rzJjv1FvsvaLadgqV4IE1vC8Guh4S9OUSMsi6Rk8-cH_OpOW6LhnXBt0Z6QMZEYBMtFR3_xcT-WlGV0HqQ_HRfJQn2Jekuks4UWoZ8uzSHR710gz33_DQX27nI_xJWt--GEr49yxVmCYdZqo2863MRLfq3VHY2bEB1MXa6JMz9V2jZoJtAuXp9RlD1T4X8OgLwkwFNUUghNQemn64TQujO0K4GdFayVUIvuf3hqc93YeKQbWZcGx7JCK5zx8xtqbpbg5zgXnStpcfKx5hKDVVkP_Af6Qb0XOdjBUi5sOIMyubfCsTDXzGdlgiNuyKUc6hTrtTpCP62glwNfAQSOq3m8pBGAhSXC7Y7I9BqXk6oJDZNgRCXSzmmBQ0ZbnfdC-EI6bGnxqyzo1CVw-BmsICIRrJ_JElWSvdWKmzYU66gg.Twjm78ttwRUq8LGaD7Bdvw",
      _puid: "user-Dq9i7EnIhzsYRuGtMvPq5xnX:1679834436-smpl3fSBX2wT8XFoMoKOJcr2N1C%2BfPH4NmDv5AWRi%2BE%3D",
      "__Secure-next-auth.callback-url": "https%3A%2F%2Fchat.openai.com%2Fchat",
      "__Host-next-auth.csrf-token": "d87f0c13b3d37a309c47dc7deda23150cb1aa156a2a59cc5f269f00bcf8f4d8c%7Ce432ce0093437a7d1beb69bb77fb251e3071ce9abb7b1db497caadad00c213e0",
      "__cf_bm": "fXkkfkE9nq70FwcHw7P7VFyLQVwDkNHaBEokaEVI9iw-1679834398-0-AWnqpDxgPasXfKrkAhwIVxZXFY0DUfy6ykdbCklR+G8xIAn5pqgptS88zDuPKNcrWmvIuJijFDD2b0yhcbQDRNWz8FFk/zop4N3cxwktJVY7kyxrjUkUg5dW6YBmkf8MGBmd9F03LIyldz7jPaKb5JmlmNEZXDmbXAroMwcNDTyX",
      "_cfuvid":"YLO6yubxpj6NTZD.C4_TbCj1xzvV9wtSfOR_lQOANNk-1679833545612-0-604800000"
    }
    let url
    url = 'https://chat.openai.com/chat'
    url = 'http://localhost:3001/process'
    axios.post(url, ctx.request.body, {
      headers: {
        Cookie: cookie
      },
      withCredentials: true
    }).then(res=>{
      ctx.body = res.data;
    }).catch(err=>{
      // console.log("错误:",err);
      ctx.body = err;
    })
   


    
  } catch (error) {
    ctx.status = 500;
    ctx.body = { error: 'An error occurred while forwarding the request.' };
  }
});

app.use(router.routes()).use(router.allowedMethods());

app.listen(3000, () => {
  console.log('Server 1 is running on http://localhost:3000');
});
